package com.learn.exchange;

import com.learn.utils.RabbitMqUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;

import java.nio.charset.StandardCharsets;

/**
 * @author DJJ_F
 * @version 1.0
 * @date 2025/08/23
 * @description ch05 交换机
 * 直接交换机
 */
public class DirectReceiver02 {
    public static final String EXCHANGE_NAME = "direct_exchange";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        // 声明队列
        String queueName = "dr02";
        //队列声明
        channel.queueDeclare(queueName, false, false, false, null);
        // 绑定队列 fanout时routingKey被忽略
        channel.queueBind(queueName, EXCHANGE_NAME, "warning");

        System.out.println("02ready-------------------");
        channel.basicConsume(queueName, true,
                (consumerTag, message) -> System.out.println("02消息：" + new String(message.getBody(), StandardCharsets.UTF_8)),
                (consumerTag) -> {});
    }
}
